package com.makejl.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class MailUtil {

	private static Logger Log = Logger.getLogger(SendMail.class);
	
	private static String server = "mail.youmo365.com";// smtp主机名。
	private static String from = "master@youmo365.com";// 发送方邮件地址
	private static String pw = "fanfan";// 发送方邮件密码。
	
	public static void send(String subject,String mailTo,String mailContent){
		Properties props = new Properties();
		Session sendMailSession;
		// Store store;
		Transport transport;
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.host", server);
		props.put("mail.smtp.user", from);
		props.put("mail.smtp.password", pw);

		sendMailSession = Session.getInstance(props, new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() // 邮件安全认证。
			{
				return new PasswordAuthentication(from, pw);
			}
		});

		Message newMessage = new MimeMessage(sendMailSession);
		try {
			try {
				newMessage.setFrom(new InternetAddress(from,"Youmo365"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				newMessage.setRecipient(Message.RecipientType.TO,
						new InternetAddress(mailTo));
			newMessage.setSubject(subject);
			newMessage.setSentDate(new Date());
			newMessage.setText(mailContent);
			transport = sendMailSession.getTransport("smtp");

			Transport.send(newMessage);

		} catch (MessagingException ex) {
			Log.fatal("feedback mail failed to:" + mailTo);
		}
	}
}
